﻿package net.wangit.context;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import net.wangit.MapList;
import net.wangit.Unit;
import net.wangit.Visitor;
import net.wangit.framework.session.AgileSession;
import net.wangit.jdbc.Table;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ActionContext实现
 * @author watson
 *
 */
@SuppressWarnings("serial")
public class ActionContextImpl implements ActionContext {
	private static final Log log = LogFactory
			.getLog(net.wangit.context.ActionContextImpl.class);
	private List<Table> tables;
	private Map<String, String[]> requestParameterMap;
	private Map<String, String[]> requestParameterMap2;
	private Map<String, Object> sessionMap;
	private Map<String, Object> sessionMap2;
	private Map<String, Object> customMap;
	private Map<String, Object> servletContextMap;
	private Map<String, File[]> files;
	private String actionParameter;
	private ActionResult actionResult;

	protected ActionContextImpl() {
		requestParameterMap2 = new HashMap<String, String[]>();
		sessionMap2 = new HashMap<String, Object>();
		customMap = new HashMap<String, Object>();
		files = new HashMap<String, File[]>();
	}

	public List<Table> getTables() {
		return tables;
	}

	public String getRequestParameter(String key) {
		return getRequestParameter(0, key);
	}

	public String getRequestParameter(String key, String defaultValue) {
		String value = getRequestParameter(key);
		return value != null ? value : defaultValue;
	}

	public String getRequestParameter(int i, String key) {
		String parameters[] = getRequestParameters(key);
		if (parameters == null)
			return null;
		if (i >= 0 && i < parameters.length) {
			return parameters[i];
		} else {
			log.warn("index overflow");
			return null;
		}
	}

	public int getRequestParameterInt(String key, int defaultValue) {
		String value = getRequestParameter(key);
		return Checker.isEmpty(value) ? defaultValue : Integer.parseInt(value);
	}

	public double getRequestParameterDouble(String key, double defaultValue) {
		String value = getRequestParameter(key);
		return Checker.isEmpty(value) ? defaultValue : Double
				.parseDouble(value);
	}

	public String[] getRequestParameters(String key) {
		String[] parameters = requestParameterMap2.get(key);
		if (parameters == null)
			parameters = requestParameterMap.get(key);
		return parameters;
	}

	public MapList getRequestUnitData(String domain, String unitId) {
		Unit unit = UnitFactory.getUnitWithoutClone(domain, unitId);
		MapList data = new MapList();
		for (int i = 0; i < unit.size(); i++) {
			String id = unit.getElement(i).getId();
			String values[] = getRequestParameters((new StringBuilder(String
					.valueOf(unitId))).append(".").append(id).toString());
			if (!Checker.isEmpty(values)) {
				for (int j = 0; j < values.length; j++)
					data.put(j, id, values[j]);

			}
			values = getRequestParameters((new StringBuilder(String
					.valueOf(unitId))).append(".").append(id).append(".k")
					.toString());
			if (!Checker.isEmpty(values)) {
				for (int j = 0; j < values.length; j++)
					data.put(j, (new StringBuilder(String.valueOf(id))).append(
							".k").toString(), values[j]);

			}
		}

		return data;
	}

	public void setRequestParameter(String key, String value) {
		String parameters[] = getRequestParameters(key);
		if (parameters == null) {
			parameters = new String[1];
			requestParameterMap2.put(key, parameters);
		}
		parameters[0] = value;
	}

	public void setRequestParameter(int i, String key, String value) {
		String parameters[] = getRequestParameters(key);
		if (parameters != null && i >= 0 && i < parameters.length)
			parameters[i] = value;
	}

	public Object getSessionAttribute(String key) {
		Object object = sessionMap2.get(key);
		if (object != null)
			return object;
		else
			return sessionMap.get(key);
	}

	public String getSessionAttribute(String key, String defaultValue) {
		Object value = getSessionAttribute(key);
		return value != null ? (String) value : defaultValue;
	}

	public void setSessionAttribute(String name, Object value) {
		sessionMap2.put(name, value);
		if (value == null)
			sessionMap.put(name, null);
	}

	public String getActionParameter() {
		return actionParameter;
	}

	public ActionResult getActionResult() {
		return actionResult;
	}

	public Visitor getVisitor() {
		return ActionContextHelper.getSession(this).getVisitor();
	}

	protected void setRequestParameterMap(Map<String, String[]> requestParameterMap) {
		this.requestParameterMap = requestParameterMap;
	}

	protected void setSessionMap(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public Map<String, Object> getSessionMap2() {
		return sessionMap2;
	}

	public Object getServletContextAttribute(String key) {
		return servletContextMap.get(key);
	}

	protected void setServletContextMap(Map<String, Object> servletContextMap) {
		this.servletContextMap = servletContextMap;
	}

	public Object getAttribute(String key) {
		return customMap.get(key);
	}

	public void setAttribute(String key, Object value) {
		customMap.put(key, value);
	}

	public void setActionParameter(String actionParameter) {
		this.actionParameter = actionParameter;
	}

	public void setActionResult(ActionResult actionResult) {
		this.actionResult = actionResult;
	}

	public void setTables(List<Table> tables) {
		this.tables = tables;
	}

	protected void setFiles(String inputName, File file[]) {
		files.put(inputName, file);
	}

	public File getFile(String key) {
		File file[] = files.get(key);
		return file != null ? file[0] : null;
	}

	public File[] getFiles(String key) {
		return files.get(key);
	}

	protected void clearSession() {
		log.debug("clearSession...");
		sessionMap.clear();
		sessionMap2.clear();
		HttpSession session = LocalContext.getLocalContext()
				.getHttpServletRequest().getSession();
		session.invalidate();
		sessionMap.put(AgileSession.KEY, new AgileSession());
	}

}
